﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.UI.HtmlControls;
using System.Web.UI;
using System.Web.Mvc.Html;
using System.Web.Mvc.Ajax;
using System.ComponentModel;
using eStore.Models;
using System.Security.Principal;

namespace eStore.Helpers
{
    public static class eStoreHtmlExtensions
    {
        public static string Button(this HtmlHelper helper, string sID, string sValue, string sclassNames, object htmlAttributes) {
            // Create tag builder
            var builder = new TagBuilder("input");

            // Create valid id
            builder.GenerateId(sID);

            // Add attributes
            builder.MergeAttribute("value", sValue);
            builder.MergeAttribute("type", "button");
            builder.MergeAttribute("class", sclassNames);
            builder.MergeAttribute("name", sID);
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

            // Render tag
            return builder.ToString(TagRenderMode.SelfClosing);
            
        }

        public static String TrimText(this HtmlHelper helper, String sText, int iMaxLength) {
            if (sText.Length > iMaxLength)
                return String.Concat(sText.Substring(0, iMaxLength), "....");
            else
                return sText;
        }

        public static string Submit(this HtmlHelper helper, string sID, string sValue, string sclassNames, object htmlAttributes)
        {
            // Create tag builder
            var builder = new TagBuilder("input");

            // Create valid id
            builder.GenerateId(sID);

            // Add attributes
            builder.MergeAttribute("value", sValue);
            builder.MergeAttribute("type", "submit");
            builder.MergeAttribute("class", sclassNames);
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

            // Render tag
            return builder.ToString(TagRenderMode.SelfClosing);


        }

        public static string Reset(this HtmlHelper helper, string sID, string sValue, string sClassNames, object htmlAttributes)
        {
            // Create tag builder
            var builder = new TagBuilder("input");

            // Create valid id
            builder.GenerateId(sID);

            // Add attributes
            builder.MergeAttribute("value", sValue);
            builder.MergeAttribute("type", "reset");
            builder.MergeAttribute("class", sClassNames);
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

            // Render tag
            return builder.ToString(TagRenderMode.SelfClosing);


        }


        public static string Image(this HtmlHelper helper, string sID, string sImageUrl, string sclassNames, object htmlAttributes)
        {
                    // Create tag builder
            var builder = new TagBuilder("image");

            // Create valid id
            builder.GenerateId(sID);

            // Add attributes
            builder.MergeAttribute("src", sImageUrl);
            builder.MergeAttribute("class", sclassNames);
            builder.MergeAttributes(new RouteValueDictionary(htmlAttributes));

            // Render tag
            return builder.ToString(TagRenderMode.SelfClosing);
        }


        public static string Image(this HtmlHelper helper, string sID, string sImageUrl, string sClassNames, bool isHref, bool launchNewPage, object htmlAttributes)
        {
            // Create tag builder
            var linkBuilder = new TagBuilder("a");            
            var sImageContent = Image(helper,sID,sImageUrl,sClassNames,htmlAttributes);
            if (isHref && launchNewPage)
                linkBuilder.MergeAttribute("target", "_new");
            else if (isHref && !(launchNewPage))
                linkBuilder.MergeAttribute("target", "_self");

            linkBuilder.MergeAttribute("href", sImageUrl);

            linkBuilder.InnerHtml = sImageContent;

            if (isHref)
                return linkBuilder.ToString(TagRenderMode.Normal);
            else
                return sImageContent;
        }

        public static string Rating(this HtmlHelper helper,String sId, int iRating, [DefaultValue(5)] int iMax, object htmlAttributes)
        {
            var spanBuilder = new TagBuilder("div");
            var radioButtons = "";
            Enumerable.Range(1, 5).ToList().ForEach(x =>
            {
                var sRadioButton = helper.RadioButton(sId, x, x == iRating, null);
                radioButtons += String.Format("{0}{1}", sRadioButton.ToHtmlString(), "&nbsp;");
            });

            spanBuilder.InnerHtml = radioButtons;

            return spanBuilder.ToString(TagRenderMode.Normal);
        }

        public static string LoginMenuItem(this HtmlHelper helper, String sId, object htmlAttributes)
        {
            

            if (helper.ViewContext.RequestContext.HttpContext.Request.IsAuthenticated)
                return helper.ActionLink("Logout", "Logout", "Account",null,htmlAttributes).ToHtmlString();
            else
                return helper.ActionLink("LogIn", "LogOn", "Account",null, htmlAttributes).ToHtmlString();

        }


    }
}